<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.sql.*"
    %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>新增會員</title>
</head>
<body>
<% if(session.getAttribute("access") == "y" ){%>
<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
<%
String mid = new String(request.getParameter("id").getBytes("iso-8859-1"),"utf-8"); //防止亂碼
String pw = new String("");
String sendpw = new String("");
String mname=new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
String msex= new String(request.getParameter("sex").getBytes("iso-8859-1"),"utf-8");
String maddress= new String(request.getParameter("address").getBytes("iso-8859-1"),"utf-8");
String mphone= new String(request.getParameter("phone").getBytes("iso-8859-1"),"utf-8");
String email= new String(request.getParameter("email").getBytes("iso-8859-1"),"utf-8");
String dep= new String(request.getParameter("dep").getBytes("iso-8859-1"),"utf-8");
String por_name= new String(request.getParameter("por_name").getBytes("iso-8859-1"),"utf-8");
String level_id= new String(request.getParameter("level_id").getBytes("iso-8859-1"),"utf-8");

//產生亂數密碼
   int i; 
   int[] rad = new int[6];  
   for(i = 0; i < 6; i++){ 
     if(i < 6){ //放小寫英文 
        rad[i] = ((int)((Math.random()*26) + 97));
      }
    } 
    	
      System.out.print("password = "); 
    for(i = 0; i < 6; i++){ 
      pw+=String.valueOf((char)rad[i]); 
   }
   sendpw = pw; //把未加密的密碼記起來，放到變數中以寄給會員
//產生亂數密碼 END
%>

<jsp:useBean id="md5" scope="page" class="bean.MD5Bean"/>

<%
pw = md5.getMD5(pw); //將密碼加密
 
String sql="INSERT INTO MEMBER(M_ID,M_NAME,PW,SEX,ADDRESS,PHONE,EMAIL,DEPARTMENT,POR_NAME,LEVEL_ID)"+
" VALUES('"+mid+"','"+mname+"','"+pw+"','"+msex+"','"+maddress+"','"+mphone+"','"+email+"','"+dep+"','"+por_name+"','"+level_id+"')";
mydb.executeInsert(sql);
%>

<jsp:useBean id="sendmail" scope="page" class="bean.SendMail"/><%//寄發mail %>
<jsp:useBean id="member" scope="page" class="myclass.Member"/> <%//取得會員資料 %>
<%
//寄信給會員

sendmail.get_pto(email);//取得會員信箱
sendmail.setSubject("member"); //郵件標題
String sqlmail="select * from SYS_SETTING WHERE S_NAME='add_mem_body'";
      ResultSet rs=mydb.executeQuery(sqlmail);
      if(rs.next()){
      	String mag = "";
      	mag = rs.getString(3);
        mag = mag+"您的帳號為:"+mid+"  您的密碼為:"+sendpw;    
		sendmail.setText(mag);
		sendmail.sendMail();
      }
response.sendRedirect("listmember.jsp");
%>

<%
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>
</body>
</html>